<?php

namespace App\Admin\Controllers\Common;

use App\Models\CommonFinancialFlow;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class FinancialFlowController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '平台财务流水';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CommonFinancialFlow());

        $grid->column('id', ___('table.zy_common_financial_flow.Id'));
        $grid->column('uid', ___('table.zy_common_financial_flow.uid'))->display(function ($id){
            return User::find($id)->name;
        });
        $grid->column('no', ___('table.zy_common_financial_flow.No'));
        $grid->column('type', ___('table.zy_common_financial_flow.Type'))->display(function ($id){
            return CommonFinancialFlow::$type[$id];
        });
        $grid->column('price', ___('table.zy_common_financial_flow.Price'));
        $grid->column('balance', ___('table.zy_common_financial_flow.balance'));
        $grid->column('external_id', ___('table.zy_common_financial_flow.External id'))->help('如支付宝流水或微信流水号');
        $grid->column('create_time', ___('table.zy_common_financial_flow.Create time'));
        $grid->column('update_time', ___('table.zy_common_financial_flow.Update time'));
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CommonFinancialFlow::findOrFail($id));

        $show->field('id', ___('table.zy_common_financial_flow.Id'));
        $show->field('no', ___('table.zy_common_financial_flow.No'));
        $show->field('type', ___('table.zy_common_financial_flow.Type'));
        $show->field('price', ___('table.zy_common_financial_flow.Price'));
        $show->field('external_id', ___('table.zy_common_financial_flow.External id'));
        $show->field('create_time', ___('table.zy_common_financial_flow.Create time'));
        $show->field('update_time', ___('table.zy_common_financial_flow.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CommonFinancialFlow());

        $form->text('no', ___('table.zy_common_financial_flow.No'));
        $form->select('uid', ___('table.zy_common_financial_flow.uid'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user')->help("输入真实用户姓名");

        $form->radio('type', ___('table.zy_common_financial_flow.Type'))->options(
            CommonFinancialFlow::$type
        );
        $form->decimal('price', ___('table.zy_common_financial_flow.Price'));
        $form->text('external_id', ___('table.zy_common_financial_flow.External id'))->help('订单ID、充值ID、提现申请ID');
        //$form->number('create_time', ___('table.zy_common_financial_flow.Create time'));
        //$form->number('update_time', ___('table.zy_common_financial_flow.Update time'));

        return $form;
    }
}
